package com.ikun.common.message;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @ClassName SendMessage  //类名称
 * @Description: 类描述
 * @Author: ximeng    //作者
 * @CreateDate: 2024/1/2 23:46	//创建时间
 * @UpdateUser: 更新人
 * @UpdateDate: 2024/1/2 23:46	//更新时间
 * @UpdateRemark: 更新的信息
 * @Version: 1.0    //版本号
 */
@Component
public class SendMessage {

    private static final String URL = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
    private static final String ACCOUNT = "C72022486";
    private static final String PASSWORD = "4fde444fa873b544e7bd07ed84bc06e8";

    public  void sendVerificationCode(String mobile, String mobil_code) {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(URL);

        client.getParams().setContentCharset("GBK");
        method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=GBK");
        String content = "你的短信验证码是："+mobil_code+"，有效时长两分半，请勿告知他人。";
        NameValuePair[] data = {
                new NameValuePair("account", ACCOUNT),
                new NameValuePair("password", PASSWORD),
                new NameValuePair("mobile", mobile),
                new NameValuePair("content", content)
        };
        method.setRequestBody(data);

        try {
            client.executeMethod(method);

            String submitResult = method.getResponseBodyAsString();

            Document doc = DocumentHelper.parseText(submitResult);
            Element root = doc.getRootElement();

            String code = root.elementText("code");
            String msg = root.elementText("msg");
            String smsid = root.elementText("smsid");

            System.out.println("发送结果：" + msg);

            if ("2".equals(code)) {
                System.out.println("短信提交成功");
            }

        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }
    public static void sendVerification(String mobile, String mobil_code) {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(URL);

        client.getParams().setContentCharset("GBK");
        method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=GBK");
        String content = "你的短信验证码是："+mobil_code+"，有效时长两分半，请勿告知他人。";
        NameValuePair[] data = {
                new NameValuePair("account", ACCOUNT),
                new NameValuePair("password", PASSWORD),
                new NameValuePair("mobile", mobile),
                new NameValuePair("content", content)
        };
        method.setRequestBody(data);

        try {
            client.executeMethod(method);

            String submitResult = method.getResponseBodyAsString();

            Document doc = DocumentHelper.parseText(submitResult);
            Element root = doc.getRootElement();

            String code = root.elementText("code");
            String msg = root.elementText("msg");
            String smsid = root.elementText("smsid");

            System.out.println("发送结果：" + msg);

            if ("2".equals(code)) {
                System.out.println("短信提交成功");
            }

        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }

}
